package com.shipwe.loans.ui.fragment

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.shipwe.adapter.ShipweRecyclerViewAdapter
import com.shipwe.adapter.ShipweViewHolderHelper
import com.shipwe.loans.R
import com.shipwe.loans.databinding.FragmentPermissionBinding
import com.shipwe.loans.pro.ShipweFragment
import com.shipwe.loans.utils.AppDensityUtil
import com.shipwe.loans.utils.decoration.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/11 15:03
 * @Annotation：权限Page
 */
class FragmentPermission(): ShipweFragment() {
    lateinit var binding: FragmentPermissionBinding
    private var data: JSONObject? = null

    //权限描述列表
    private var permissionAdapter: PermissionItemAdapter? = null

    constructor(data: JSONObject): this(){
        this.data = data
    }

    override fun getShipweRootView(inflater: LayoutInflater): View {
        binding = FragmentPermissionBinding.inflate(inflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#00000000"))
        initPermissionListView()
    }

    override fun initShipweListener() {
    }

    override fun initShipweData() {
        val desList = arrayListOf<JSONObject>()
        if (data != null){
            val desArray = data!!.optJSONArray("msgList")
            if (desArray != null && desArray.length() > 0){
                for (i in 0..<desArray.length()){
                    desList.add(desArray.optJSONObject(i))
                }
            }
        }
        permissionAdapter!!.data = desList
    }

    /* 初始化列表 */
    private fun initPermissionListView(){
//        binding.permissionListView.visibility = View.GONE
//        binding.permissionListView.setHasFixedSize(true)
//        binding.permissionListView.isNestedScrollingEnabled = false
//        binding.permissionListView.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        binding.permissionListView.layoutManager = layoutManager
        val topDecoration = AppDensityUtil.dp2px(13f)
        binding.permissionListView.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        permissionAdapter = PermissionItemAdapter(binding.permissionListView)
        permissionAdapter!!.setOnItemChildClickListener { _, _, position ->
            val item = permissionAdapter!!.getItem(position)
        }
        binding.permissionListView.adapter = permissionAdapter
    }

    class PermissionItemAdapter(recyclerView: RecyclerView?) : ShipweRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.fragment_permission_item) {
        override fun setItemChildListener(helper: ShipweViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }

        override fun fillData(helper: ShipweViewHolderHelper, position: Int, model: JSONObject) {
            val showTitle = model.optString("title","")
            if (showTitle.isNotEmpty()){
                helper.setVisibility(R.id.permissionTitle,View.VISIBLE)
                helper.setText(R.id.permissionTitle,showTitle)
            } else {
                helper.setVisibility(R.id.permissionTitle,View.GONE)
            }
            helper.setText(R.id.permissionDes,model.optString("des"))
        }
    }
}